package com.liujit.common.enums;

import com.liujit.common.exception.*;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * @Description: 业务异常枚举
 * @Author: liujun
 * @Date: 2021/7/1 2:01 下午
 */
@AllArgsConstructor
public enum BusinessExceptionEnum {
    /**
     * 参数无效
     */
    PARAM_INVALID(ParamInvalidException.class, HttpStatus.OK, ResultCode.PARAM_INVALID),
    /**
     * 数据不存在
     */
    DATA_NOT_FOUND(DataNotFoundException.class, HttpStatus.OK, ResultCode.DATA_NOT_FOUND),
    /**
     * 数据已存在
     */
    DATA_EXIST(DataExistException.class, HttpStatus.OK, ResultCode.DATA_EXIST);

    private Class<? extends BusinessException> eClass;

    private HttpStatus httpStatus;

    private ResultCode resultCode;

    public Class<? extends BusinessException> getEClass() {
        return eClass;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public ResultCode getResultCode() {
        return resultCode;
    }

    public static boolean isSupportHttpStatus(int httpStatus) {
        for (BusinessExceptionEnum exceptionEnum : BusinessExceptionEnum.values()) {
            if (exceptionEnum.httpStatus.value() == httpStatus) {
                return true;
            }
        }
        return false;
    }

    public static boolean isSupportException(Class<?> z) {
        for (BusinessExceptionEnum exceptionEnum : BusinessExceptionEnum.values()) {
            if (exceptionEnum.eClass.equals(z)) {
                return true;
            }
        }
        return false;
    }

    public static BusinessExceptionEnum getByHttpStatus(HttpStatus httpStatus) {
        if (httpStatus == null) {
            return null;
        }
        for (BusinessExceptionEnum exceptionEnum : BusinessExceptionEnum.values()) {
            if (httpStatus.equals(exceptionEnum.httpStatus)) {
                return exceptionEnum;
            }
        }
        return null;
    }

    public static BusinessExceptionEnum getByEClass(Class<? extends BusinessException> eClass) {
        if (eClass == null) {
            return null;
        }
        for (BusinessExceptionEnum exceptionEnum : BusinessExceptionEnum.values()) {
            if (eClass.equals(exceptionEnum.eClass)) {
                return exceptionEnum;
            }
        }
        return null;
    }
}
